from django.shortcuts import render
from .models import Departments
from datetime import datetime
from django.http import JsonResponse, HttpResponse
from django.contrib.auth.decorators import login_required
from utils.json_schema import *
from utils.json_validate import json_validate
from users.models import Users
import json


# 创建部门
@json_validate(create_department_schema)
@login_required()
def create_department(request):
    # import pdb
    # pdb.set_trace()
    if not request.user.is_superuser:
        return HttpResponse('您不是管理员,没有权限操作此功能')
    json_str = request.body
    json_str = json_str.decode()
    req_data = json.loads(json_str)
    name = req_data["name"]
    department_manager = req_data["department_manager"]
    try:
        department = Departments()
        department.name = name
        department.create_date = datetime.now()
        department.create_user = request.user.id
        # department.create_user = "刘耀升"
        department.department_manager = department_manager
        department.save()
        data = {"success": True, "data": {"msg": "添加部门成功"}}
    except Exception as e:
        data = {"success": False, "data": {"msg": str(e)}}
    return JsonResponse(data)


# 删除部门
@login_required()
@json_validate(delete_department_schema)
def delete_department(request):
    if not request.user.is_superuser:
        data = {"success": False, "data": {"msg": "您没有权限进行删除操作"}}
        return JsonResponse(data)
    # 查询部门下是否还有员工
    json_str = request.body
    json_str = json_str.decode()
    req_data = json.loads(json_str)
    department_id = req_data["department_id"]
    department = Departments.objects.get(id=department_id)
    users_of_department = Users.objects.filter(department=department)
    if users_of_department:
        data = {"success": False, "data": {"msg": "部门下还有员工，请删除所有员工后再进行删除操作！"}}
        return JsonResponse(data)
    department.delete()
    data = {"success": True, "data": {"msg": "删除部门成功"}}
    return JsonResponse(data)


# 修改部门属性
@login_required()
@json_validate(delete_department_schema)
def update_department(request):
    json_str = request.body
    json_str = json_str.decode()
    req_data = json.loads(json_str)
    id = req_data["department_id"]
    name = req_data["name"]
    department_manager = req_data["department_manager"]
    try:
        department = Departments.objects.get(id=id)
    except Exception as e:
        return HttpResponse("无此部门,请重新选择")
    department.name = name
    department.department_manager = department_manager
    department.save()
    data = {"success": True, "data": {"msg": "修改部门信息成功"}}
    return JsonResponse(data)

# 查询所有部门
@login_required()
def get_departments(request):
    departments = Departments.objects.all()
    data_list = []
    for department in departments:
        data_dict = {}
        data_dict["id"] = department.id
        data_dict["name"] = department.name
        data_dict["department_manager"] = department.department_manager
        data_list.append(data_dict)
    data = {"success": True, "data": {"msg": "查询成功", "data": data_list}}
    return JsonResponse(data)


# 查询部门员工
@login_required()
def get_department_users(request):
    json_str = request.body
    json_str = json_str.decode()
    req_data = json.loads(json_str)
    id = req_data["department_id"]
    try:
        department = Departments.objects.get(id=id)
    except Exception as e :
        return HttpResponse("查询出错或无此部门，请重新尝试!")
    users_of_department = Users.objects.filter(department=department)
    if len(users_of_department) == 0:
        data = {"success": True, "data": {"msg": "该部门下无员工"}}
        return JsonResponse(data)
    data_list = []
    for user in users_of_department:
        data_dict = {}
        data_dict["user_id"] = user.id
        data_dict["last_login"] = user.last_login
        data_dict["email"] = user.email
        data_dict["date_joined"] = user.date_joined
        data_dict["mobile"] = user.mobile
        data_dict["name"] = user.name
        data_dict["department_manager"] = department.department_manager
        data_list.append(data_dict)
    data = {"success": True, "data": {"msg": "查询成功", "data": data_list}}
    return JsonResponse(data)



